class Solution {
public:
    int target;
    int sum = 0;
    vector<int> path;
    vector<vector<int>> ret;

    vector<vector<int>> combinationSum(vector<int>& candidates, int _target) {
        target = _target;
        dfs(candidates, 0);
        return ret;
    }

    void dfs(vector<int>& candidates, int pos)
    {
        if (sum == target)
        {
            ret.push_back(path);
            return;
        }

        if (sum > target) return;

        for (int i = pos; i < candidates.size(); i++)
        {
            path.push_back(candidates[i]);
            sum += candidates[i];
            dfs(candidates, i);
            sum -= candidates[i];
            path.pop_back();
        }
    }
};